//
//  HRShowLabelView.m
//  Hear
//
//  Created by mac on 2017/11/13.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRShowLabelView.h"
#import "HRLabelCell.h"
@interface HRShowLabelView ()
@property (nonatomic, copy)SelectLabelBlock selectIndexBlock;
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSArray *contentArr;
@property(nonatomic, assign)int selectCount;

@end
@implementation HRShowLabelView

+(void)showLabelView:(NSArray *)contentArr selectIndexBlock:(SelectLabelBlock)selectIndexBlock
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    HRShowLabelView * showView = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
    showView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    id view = [[window subviews] lastObject];
    if([view isKindOfClass:[HRShowLabelView class]]){
        
        
        HRShowLabelView *shareView = (HRShowLabelView *)view;
        [shareView closeView];
        
    }else{
        [window addSubview:showView];
        
        [showView loadView:contentArr selectIndexBlock:selectIndexBlock];
        
    }
}
- (void)loadView:(NSArray *)contentArr selectIndexBlock:(SelectLabelBlock)selectIndexBlock{
    self.selectIndexBlock = selectIndexBlock;
    
    _contentArr = contentArr;
    

    
//    self.contentArr = @[@"随性",@"敢爱敢恨",@"喜欢简单",@"呆萌",@"长发及腰",@"讲义气",@"御姐",@"理想主义",@"老司机",@"文艺"];
    
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeView)];
//    [self addGestureRecognizer:tap];
    
    self.alpha = 0;
    CGFloat pading = 25;
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(pading, pading, kFullWidth-pading*2, kFullHeight-pading*2)];
    contentView.backgroundColor = [UIColor whiteColor];
    [self addSubview:contentView];
    contentView.alpha = 0;
    contentView.layer.cornerRadius = 8;
    contentView.clipsToBounds = YES;
    
    self.layer.affineTransform = CGAffineTransformMakeScale(0.1, 0.1);
    [UIView animateWithDuration: 0.25 animations:^{
        self.layer.affineTransform = CGAffineTransformMakeScale(1.0, 1.0);
        self.alpha = 1;
        contentView.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
    
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"标签";
    titleLabel.font = CustomUIFont(17);
    titleLabel.textColor = FirstTextColor;
    [contentView  addSubview:titleLabel];
    titleLabel.frame = CGRectMake(pading, pading, contentView.width-pading*2, 20);
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.frame = CGRectMake(titleLabel.left, titleLabel.bottom+pading, titleLabel.width, contentView.height - titleLabel.bottom-90);
    self.tableView.rowHeight = 40;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [contentView addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;
    
    UIButton *enterButton = [UIButton buttonWithType:0];
    [enterButton setTitle:@"确定" forState:0];
    [enterButton setTitleColor:BlueColor forState:0];
    enterButton.titleLabel.font = CustomUIFont(17);
    [enterButton addTarget:self action:@selector(enterButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:enterButton];
    enterButton.frame = CGRectMake(contentView.width-60,self.tableView.bottom+pading, 40, 25);
    
    
    UIButton *cancelButton = [UIButton buttonWithType:0];
    [cancelButton setTitle:@"取消" forState:0];
    [cancelButton setTitleColor:FirstTextColor forState:0];
    cancelButton.titleLabel.font = CustomUIFont(17);
    [cancelButton addTarget:self action:@selector(cancelButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:cancelButton];
    cancelButton.frame = CGRectMake(enterButton.left-enterButton.width-pading, enterButton.top, enterButton.width, enterButton.height);
    
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.contentArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HRLabelCell *cell = [HRLabelCell cellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.contentArr[indexPath.row];
    WeakSelf
    return cell;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (void)enterButtonClick{
    NSMutableArray *selectArr = [NSMutableArray array];
    for (HRUserLabelModel *model in self.contentArr) {
        if (model.isSelect){
            [selectArr addObject:model.content];
        }
    }
    self.layer.affineTransform = CGAffineTransformMakeScale(1.0, 1.0);
    
    [UIView animateWithDuration: 0.25 animations:^{
        self.layer.affineTransform = CGAffineTransformMakeScale(0.1, 0.1);
        
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        self.selectIndexBlock(selectArr);
    }];
}
- (void)cancelButtonClick{
    [self closeView];
}


-(void)closeView
{
    self.layer.affineTransform = CGAffineTransformMakeScale(1.0, 1.0);
    
    [UIView animateWithDuration: 0.25 animations:^{
        self.layer.affineTransform = CGAffineTransformMakeScale(0.1, 0.1);
        
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
@end
